-module(client).
-export([start/0]).

-import(werkzeug,[logging/2, get_config_value/2, timeMilliSecond/0, to_String/1, contains/2]).

start() -> 
	initialize().

initialize() ->
	{_,Handle} = file:consult("../config/client.cfg"),
	{_,Clients} = get_config_value(clients, Handle),
	{_,Lifetime} = get_config_value(lifetime, Handle),
	{_,Servername} = get_config_value(servername, Handle),
	{_,Sendeintervall} = get_config_value(sendeintervall, Handle),
	{_,RegisteredServer} = get_config_value(registeredServer, Handle),
	start_clients({RegisteredServer,Servername}, Sendeintervall, Lifetime, Clients).

start_clients(_,_,_,0) -> ok;
start_clients(Server, Sendeintervall, Lifetime, Clients) ->
	PID = spawn(fun() -> main(Server, Sendeintervall, Clients-1) end),%woher?
	timer:send_after(Lifetime*1000,PID,game_over),
	start_clients(Server, Sendeintervall, Lifetime, Clients-1).

main({ServerReg, Servername}, Sendeintervall, ClientID) ->
	net_adm:ping(Servername),
	timer:send_after(Sendeintervall*1000, self(), be_creative),%self = PID
	%{_,Hostname} = inet:gethostname(),
	log_message(ClientID, lists:concat([to_String(ClientID), "-", node(), "-", to_String(self()), "-309 Start: ", timeMilliSecond(), "|."])),
	main({ServerReg, Servername}, Sendeintervall, [], 0, ClientID).


main(Server, Sendeintervall, EigeneNachrichten, GesendeteNachrichten, ClientID) ->
	receive
		game_over -> bye;
		be_creative-> Server ! {getmsgid, self()}, 
					  main(Server, Sendeintervall, EigeneNachrichten, GesendeteNachrichten, ClientID);
		{nnr, NachrichtenId} when GesendeteNachrichten < 5 ->
				Log_Nachricht = get_Log_Nachricht(ClientID, NachrichtenId),
				Server ! {dropmessage, {Log_Nachricht, NachrichtenId}},

				log_message(ClientID, Log_Nachricht),
				timer:send_after(Sendeintervall*1000, self(), be_creative),
				main(Server, Sendeintervall , [NachrichtenId | EigeneNachrichten], GesendeteNachrichten+1, ClientID);
		{nnr, NachrichtenId} ->
				%Loggen, dass Nachricht vergessen wurde, fragt NachrichtenID ab, sendet nix
				Server  ! {getmessages, self()},
				log_message(ClientID, lists:concat([to_String(NachrichtenId),"te_Nachricht um ", timeMilliSecond(), "| vergessen zu senden ******\n" ])),
				get_messages(Server, EigeneNachrichten, ClientID),
				log_message(ClientID, "..getmessages..Done\n"),
				timer:send_after(Sendeintervall*1000, self(), be_creative),
				flush_messages(),
				main(Server, next_sendeintervall(Sendeintervall, ClientID), EigeneNachrichten, 0, ClientID)
	end.


next_sendeintervall(Sendeintervall, ClientID) ->
	NeuesIntervall = round(case random:uniform(2) of
			1 when Sendeintervall*0.5 < 1 -> 1;
			1 -> Sendeintervall * 0.5;
			2 -> Sendeintervall * 1.5
	end),
	log_message(ClientID,lists:concat(["Neues Sendeintervall: ", NeuesIntervall, "Sekunden", "(", Sendeintervall, ")\n"])),
	NeuesIntervall.

get_messages(Server, EigeneNachrichten, ClientID) ->
	receive
		{reply, NachrichtenId, Nachricht, true} -> 
				case contains(NachrichtenId, EigeneNachrichten) of
					true -> log_message(ClientID, lists:concat([Nachricht, " ******\n"]));%
					false -> log_message(ClientID, Nachricht)
				end;
		{reply, NachrichtenId, Nachricht, false} -> 
				Server ! {getmessages, self()}, 
				case contains(NachrichtenId, EigeneNachrichten) of
					true -> log_message(ClientID, lists:concat([Nachricht, " ******\n"]));%
					false -> log_message(ClientID, Nachricht)
				end,
				get_messages(Server, EigeneNachrichten, ClientID)
	end.


get_Log_Nachricht(ClientID, NachrichtenId) ->
	%{_,Hostname} = inet:gethostname(),
	lists:concat([to_String(ClientID),"-", node(),"-", to_String(self()), "-309: ", NachrichtenId, "te_Nachricht. C Out:",timeMilliSecond(), "| "]).

log_message(ClientID, Nachricht) ->
	%{_,Hostname} = inet:gethostname(),
	logging(lists:concat(["../Client_ ", ClientID, node(), ".log"]), lists:concat([Nachricht,"\n"])).


flush_messages() ->
	receive 
		{nnr,_} -> flush_messages()
	after 0 -> ok
	end.

